package bullet;

import input.AbstractInput;
import input.AbstractInput.direction;

import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.PriorityQueue;

import javax.imageio.ImageIO;

import tanks.PlayerTank;
import base_object.ABaseObject;
import map.Map;
import duck.ABlock;

public class Bullet extends ABlock {

	BufferedImage oldImage, newImage;
	BufferedImage[] bullet = new BufferedImage[4];
	AbstractInput aI;
	Map map;
	int step = 5;
	direction dir;

	public Bullet(int x, int y, AbstractInput aI, Map map, direction v) {
		super(map);

		super._x = x + 12 + 24 * v.x;
		super._y = y + 12 + 24 * v.y;
		this.aI = aI;
		this.map = map;
		dir = v;
		setSize(24);
		setPriority(1);
		rect = new Rectangle(_x, _y, _size, _size);
		try {
			FileInputStream image = new FileInputStream(
					"C:\\Users\\monushka\\Desktop\\BattleCity\\bullet.png");
			oldImage = ImageIO.read(image);
			image.close();
			bullet[0] = rotate(oldImage, 90);// right
			bullet[1] = rotate(oldImage, -90);// left
			bullet[2] = rotate(oldImage, 180);// down
			bullet[3] = oldImage;// up
			newImage = oldImage;
		} catch (IOException ex) {
			System.out.println(ex.toString());
		}
	}

	public void move() {
		rect = new Rectangle(_y + dir.y * step, _x + dir.x * step, _size, _size);
		if (dir.ordinal() < bullet.length)
			newImage = bullet[dir.ordinal()];

		boolean result = false;
		for (Iterator<ABaseObject> i = map.getQueue().iterator(); i.hasNext();) {
			ABaseObject temp = i.next();
			if ((!this.equals(temp)) && (temp.getPriority() != 0))
				result = intersects(temp.getRectangle());
			if ((result) || (_x == 0) || (_x == 600) 
						 || (_y == 0) || (_y == 648)) {
				toKill = true;
				return;
			}
		}

		super._x += dir.x * step;
		super._y += dir.y * step;

		if (super._x < 0)
			super._x = 0;
		if (super._y < 0)
			super._y = 0;
		if (super._x > 600)
			super._x = 600;
		if (super._y > 648)
			super._y = 648;
	}

	@Override
	public void draw(Graphics g) {
		g.drawImage(newImage, _x, _y, null);
	}

}
